package com.aaa.tmr.service.impl;

import com.aaa.tmr.entity.Custom;
import com.aaa.tmr.entity.Visit;
import com.aaa.tmr.dao.VisitDao;
import com.aaa.tmr.listener.DataListener;
import com.aaa.tmr.service.VisitService;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (Visit)表服务实现类
 *
 * @author makejava
 * @since 2021-03-24 09:47:52
 */
@Service("visitService")
public class VisitServiceImpl implements VisitService {
    @Resource
    private VisitDao visitDao;
    @Resource
    private HttpSession session;

    /**
     * 通过ID查询单条数据
     *
     * @param visitId 主键
     * @return 实例对象
     */
    @Override
    public Visit queryById(Integer visitId) {
        return this.visitDao.queryById(visitId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Visit> queryAllByLimit(int offset, int limit) {
        return this.visitDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param visit 实例对象
     * @return 实例对象
     */
    @Override
    public Visit insert(Visit visit) {
        this.visitDao.insert(visit);
        return visit;
    }

    /**
     * 修改数据
     *
     * @param visit 实例对象
     * @return 实例对象
     */
    @Override
    public Visit update(Visit visit) {
        this.visitDao.update(visit);
        return this.queryById(visit.getVisitId());
    }

    /**
     * 通过主键删除数据
     *
     * @param visitId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer visitId) {
        return this.visitDao.deleteById(visitId) > 0;
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param visit 实例对象
     * @return 对象列表
     */
    @Override
    public PageInfo queryAll(int pageNo, int pageSize, Visit visit) {

        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<Visit>(this.visitDao.queryAll(visit));
    }

    /**
     * 通过主键批量删除数据
     *
     * @param visitIds 主键
     * @return 影响行数
     */
    @Override
    public int batchDeleteById(String visitIds) {

        return visitDao.batchDeleteById(visitIds);
    }

    @Override
    public boolean importVisit(MultipartFile file) {
            ExcelReader excelReader = null;
            try {
                //实例化监听对象
                DataListener dataListener =new DataListener();
                //读excel数据到excelReader对象中
                excelReader = EasyExcel.read(file.getInputStream(), Visit.class,dataListener).build();
                ReadSheet readSheet = EasyExcel.readSheet(0).build();
                excelReader.read(readSheet);
                //定义标识符
                boolean isSuc =true;
                //获取读取到的数据列表
                List list =  dataListener.getList();
                //判断是否为空
                if(!CollectionUtils.isEmpty(list)){
                    //循环对象
                    for (Object o : list) {
                        Visit visit =(Visit) o;
                        Visit returnVisit = this.insert(visit);
                        if(returnVisit==null){
                            isSuc=false;
                        }
                    }
                }
                return isSuc;
            }catch (Exception e){
                e.printStackTrace();
            } finally {
                if (excelReader != null) {
                    // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                    excelReader.finish();
                }
            }
            return false;

    }

    /**
     * 按年月查询图标显示数据
     *
     * @param year
     * @param month
     * @return
     */
    @Override
    public List<Map> queryShowChartData(int year, int month) {
        return visitDao.queryShowChartData(year,month);
    }
}
